//
//  AddProjectExperienceVC.m
//  MyProject
//
//  Created by jiaming yan on 2017/9/13.
//  Copyright © 2017年 yanjiaming. All rights reserved.
//

#import "AddProjectExperienceVC.h"
#import "IQTextView.h"
//#import "TimeSelectView.h"
#import "GetResumeByUserIdProjectExperienceList.h"
#import "TypePickerView.h"
@interface AddProjectExperienceVC ()
{
    CGFloat _floatBegin;
    CGFloat _floatEnd;
}
@property (weak, nonatomic) IBOutlet UITextField *projectNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *jobTextField;
@property (weak, nonatomic) IBOutlet UITextField *beginTimeTextField;
@property (weak, nonatomic) IBOutlet UITextField *endTimeTextField;
@property (weak, nonatomic) IBOutlet IQTextView *desTextView;
//@property (copy, nonatomic) NSString *strBeginTimeChuo;
//@property (copy, nonatomic) NSString *strEndTimeChuo;
@property (weak, nonatomic) IBOutlet UIView *viewBtnDeleteBg;

@end

@implementation AddProjectExperienceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"编辑项目经历";
    _floatBegin = 0;
    _floatEnd = 0;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"保存" forState:UIControlStateNormal];
    [button setTitleColor:UIColorFromRGB(0x13bacd) forState:UIControlStateNormal];
    button.titleLabel.font = k15Font;
    button.frame = CGRectMake(0, 0, 40, 40);
    [button addTarget:self action:@selector(saveButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    
    negativeSpacer.width = -10;
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,barItem];
    if (IOS11Later) {
        button.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
    }
    if (!self.model) {
        //如果是增加,需要隐藏最底端按钮
        self.viewBtnDeleteBg.hidden = YES;
    }else{
        //否则,需要把原始值付进来
        [self setValues];
    }
}

- (void)setValues{
    self.projectNameTextField.text = self.model.projectName;
    self.jobTextField.text= self.model.duty;
    self.beginTimeTextField.text = self.model.startTime;
    self.endTimeTextField.text = self.model.endTime;
    self.desTextView.text = self.model.projectDescribe;
}

-(void)saveButtonClick{
    
    NSString *strAlert = @"";
    
    if (self.projectNameTextField.text.length == 0) {
        strAlert = @"请输入项目名称";
    }else if (self.jobTextField.text.length == 0){
        strAlert = @"请输入你的职责";
    }else if (self.beginTimeTextField.text.length == 0){
        strAlert = @"请选择开始时间";
    }else if (self.endTimeTextField.text.length == 0){
        strAlert = @"请选择结束时间";
    }else if (self.desTextView.text.length == 0){
        strAlert = @"请输入项目描述";
    }
    if (strAlert.length != 0) {
        [CommonUtils showToast:strAlert];
        return;
    }
    
    NSLog(@"保存点击");
    NSMutableDictionary *dicModelNow = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
                                          self.projectNameTextField.text,@"projectName",
                                          self.jobTextField.text,@"duty",
                                          self.beginTimeTextField.text,@"startTime",
                                          self.endTimeTextField.text,@"endTime",
                                          self.desTextView.text,@"projectDescribe",
                                          nil];
    
    [self showProgressWithView:self.view animated:YES];
    if (self.model) {
        dicModelNow = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
                       [NSNumber numberWithInteger:self.model.projectExperienceId],@"projectExperienceId",
                       self.projectNameTextField.text,@"projectName",
                       self.jobTextField.text,@"duty",
                       self.beginTimeTextField.text,@"startTime",
                       self.endTimeTextField.text,@"endTime",
                       self.desTextView.text,@"projectDescribe",
                       nil];

    }
    [YJMSendRequest postDataWithParameters:dicModelNow url:@"resume/updateProjectExperience" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

- (IBAction)btnDeleteOnClick:(id)sender {
    NSDictionary *dicModel = [[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInteger:self.model.projectExperienceId],@"projectExperienceId",nil];
    [self showProgressWithView:self.view animated:YES];
    
    [YJMSendRequest postDataWithParameters:dicModel url:@"resume/deleteProjectExperience" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];

}


-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    __weak typeof(self) weakSelf = self;
    NSDate* _date = [NSDate date];
    /*添加你自己响应代码*/
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy"];
    //用[NSDate date]可以获取系统当前时间
    NSString *currentDateStr = [dateFormatter stringFromDate:_date];
    NSMutableArray *yearArr = [NSMutableArray array];
    
    for (NSInteger i = 0; i < 60; i++) {
        [yearArr addObject:[NSString stringWithFormat:@"%zd",[currentDateStr integerValue] - i]];
    }
    if (textField == self.beginTimeTextField) {
        [self.view endEditing:YES];
        
        TypePickerView *time = [[TypePickerView alloc]init];
        time.typeArr = yearArr;
        time.typeArr1 = @[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12"];
        time.selectPicker = ^(NSString *timeStr){
            
            if ([timeStr floatValue]>_floatEnd && _floatEnd!=0) {
                [CommonUtils showToast:@"入职时间不能晚于离职时间"];
                return;
            }
            _floatBegin = [timeStr floatValue];
            weakSelf.beginTimeTextField.text = [NSString stringWithFormat:@"%@",timeStr];
        };

        [time creatTypePickerView:self.view];
        return NO;
    }else if (textField == self.endTimeTextField) {
        [self.view endEditing:YES];
        TypePickerView *time = [[TypePickerView alloc]init];
        time.typeArr = yearArr;
        time.typeArr1 = @[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12"];
        time.selectPicker = ^(NSString *timeStr){
            
            if ([timeStr floatValue]<_floatBegin ) {
                [CommonUtils showToast:@"离职时间不能早于入职时间"];
                return;
            }
            weakSelf.endTimeTextField.text = [NSString stringWithFormat:@"%@",timeStr];
        };
        
        [time creatTypePickerView:self.view];
        return NO;
    }
    UIView *tempView = [self.view viewWithTag:909090901];
    [tempView removeFromSuperview];
    return YES;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    UIView *tempView = [self.view viewWithTag:909090901];
    [tempView removeFromSuperview];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
